package ssq.gamest.wolfmankill;

import net.sf.json.JSONObject;

import org.java_websocket.WebSocket;

public class Participant {
  public WebSocket socket;
  public String name;
  public Status status;
  public Role role;
  public int voteCnt = 0;
  public boolean isSergent = false;

  @Override
  public String toString() {
    return this.name;
  }

  protected void resetVoteCnt() {
    this.voteCnt = 0;
  }

  public Status getStatus() {
    return this.status;
  }

  public void setStatus(Status status) {
    this.status = status;
  }

  public Role getRole() {
    return this.role;
  }

  public void setRole(Role role) {
    this.role = role;
  }

  public WebSocket getSocket() {
    return this.socket;
  }

  public void setSocket(WebSocket socket) {
    this.socket = socket;
  }

  public String getName() {
    return this.name;
  }

  String toJson() {
    JSONObject result = new JSONObject();

    result.put("name", this.name);
    result.put("status", this.status.toString());
    result.put("role", this.role.toString());
    result.put("isSergent", String.valueOf(isSergent));
    return result.toString();
  }

  public void fromJson(JSONObject json) {
    this.name = json.getString("name");
    this.role = Role.valueOf(json.getString("role"));
    this.status = Status.valueOf(json.getString("status"));
    this.isSergent = Boolean.valueOf(json.getString("isSergent"));
  }

  public Participant(WebSocket socket, String name) {
    this.socket = socket;
    this.name = name;
  }

  public Participant(String name) {
    this.name = name;
  }

  public String toFullString() {
    StringBuilder sb = new StringBuilder();
    sb.append("\nname: ").append(this.name).append("\nstatus: ").append(this.status).append("\nrole: ").append(this.role);
    return sb.toString();
  }

  public void forget() {
    this.role = Role.outsider;
  }

  public static enum Role {
    长老("elder"), 盗贼("robber"), 丘比特("cupid"), 狼人("wolf"), 预言家("predicter"), 守卫("protector"), 女巫("witch"), 平民("civilian"), 猎人("hunter"), outsider("outsider");

    String englishName;

    public String getEnglishName() {
      return englishName;
    }

    private Role(String eng) {
      englishName = eng;
    }

    public boolean isGoodMan() {
      return this != 狼人;
    }
  }

  public static enum Status {
    alive, half_alive, dead;
  }
}
